package com.se.service;

import cn.hutool.core.util.StrUtil;
import com.se.dao.mysql.*;
import com.se.domain.mysql.ActivityDO;
import com.se.domain.mysql.ApplyDO;
import com.se.domain.mysql.UserDO;
import com.se.domain.qo.ApplyActQO;
import com.se.domain.request.param.ListUserActParam;
import com.se.domain.vo.*;
import com.se.mapper.ApplyActMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

@Service
public class UserService {
    @Autowired
    UserDao userDao;
    @Autowired
    ActDao actDao;
    @Autowired
    ApplyDao applyDao;
    @Autowired
    CatDao catDao;
    @Autowired
    ApplyActMapper applyActMapper;
    @Autowired
    ImgDao imgDao;

    public UserVO getUserInfo(Long userId) {
        return userDao.getVOByUserId(userId);
    }

    public List<UserActItemVO> listUserAct(ListUserActParam param, Long userId) {


        List<UserActItemVO> res = new ArrayList<>();
        Set<Long> catIdSet = new HashSet<>();
        List<Long> imgIdList = new ArrayList<>();
        switch (param.getType()){
            //发布的活动
            case "publish" -> {
                if(StrUtil.isBlank(param.getSort())){
                    param.setSort("act_id");
                }else {
                    switch (param.getSort()) {
                        case "start" -> {
                            param.setSort("start_time");
                        }
                        case "end" -> {
                            param.setSort("end_time");
                        }
                    }
                }

                List<ActivityDO> activityDOList = actDao.listByUserId(userId, param.getSort(), param.getDesc(), param.getPage().getPage(), param.getPage().getSize());
                if(activityDOList.isEmpty()){
                    return Collections.emptyList();
                }

                UserVO userVO = userDao.getVOByUserId(userId);
                userVO.setSelf(true);

                for (ActivityDO activityDO : activityDOList) {

                    ActVO actVO = new ActVO(activityDO);

                    for (String catIdStr : activityDO.getCatIds().split(",")) {
                        Long catId = Long.parseLong(catIdStr);
                        catIdSet.add(catId);
                        actVO.getCatIds().add(catId);
                    }

                    for (Long imgId : activityDO.getImgIdList()) {
                        imgIdList.add(imgId);
                        actVO.getImgIds().add(imgId);
                    }

                    UserActItemVO itemVO = new UserActItemVO(userVO, actVO);
                    res.add(itemVO);
                }
            }
            case "apply" -> {

                if(StrUtil.isBlank(param.getSort())){
                    //按照apply_id排序
                    param.setSort("apply_apply_id");
                }else {
                    switch (param.getSort()){
                        case "start" -> param.setSort("activity_start_time");
                        case "end" -> param.setSort("activity_end_time");
                    }
                }

                List<ApplyActQO> qoList = applyActMapper.listActIdByUserId(userId, param.getSort(), param.getDesc(),
                        param.getPage().limitStart(), param.getPage().getSize());

                if(qoList.isEmpty()){
                    return Collections.emptyList();
                }
                List<Long> userIds = new ArrayList<>();
                for (ApplyActQO applyActQO : qoList) {

                    userIds.add(applyActQO.getAct().getUserId());

                    UserActItemVO itemVO = new UserActItemVO();

                    itemVO.setApply(new ApplyVO(applyActQO.getApply()));
                    itemVO.setAct(new ActVO(applyActQO.getAct()));
                    itemVO.setUserId(applyActQO.getAct().getUserId());

                    for (String catIdStr : applyActQO.getAct().getCatIds().split(",")) {
                        Long catId = Long.parseLong(catIdStr);
                        catIdSet.add(catId);
                        itemVO.getAct().getCatIds().add(catId);
                    }
                    for (Long imgId : applyActQO.getAct().getImgIdList()) {
                        imgIdList.add(imgId);
                        itemVO.getAct().getImgIds().add(imgId);
                    }

                    res.add(itemVO);
                }

                //填充user信息
                Map<Long, UserVO> userVOMap = userDao.mapVOByUserIds(userIds);
                for (UserActItemVO itemVO : res) {
                    UserVO userVO = userVOMap.get(itemVO.getUserId());
                    userVO.checkSelf(userId);
                    itemVO.setUser(userVO);

                }
            }
        }
        //填充cat信息
        Map<Long, CatVO> catVOMap = catDao.mapVOByCatIds(catIdSet);
        for (UserActItemVO itemVO : res) {
            for (Long catId : itemVO.getAct().getCatIds()) {
                CatVO catVO = catVOMap.get(catId);
                if(catVO == null)
                    continue;
                itemVO.getAct().getCats().add(catVO);
            }
        }

        //填充img信息
        Map<Long, ImgVO> imgVOMap = imgDao.mapVOByImgIdList(imgIdList);
        for (UserActItemVO itemVO : res) {
            for (Long imgId : itemVO.getAct().getImgIds()) {
                ImgVO imgVO = imgVOMap.get(imgId);
                if(imgVO == null)
                    continue;
                itemVO.getAct().getImgs().add(imgVO);
            }
        }

        return res;
    }
}
